package br.com.lh.urabuilder.command
{
	import br.com.lh.urabuilder.domain.xml.Option;
	import br.com.lh.urabuilder.domain.xml.UraXml;
	import br.com.lh.urabuilder.event.AddActionEvent;
	import br.com.lh.urabuilder.event.LinkActionEvent;
	import br.com.lh.urabuilder.event.LoadUraEvent;
	import br.com.lh.urabuilder.event.NewUraEvent;
	import br.com.lh.urabuilder.event.SaveConditionalPropertiesEvent;
	import br.com.lh.urabuilder.event.SavePropertiesEvent;
	import br.com.lh.urabuilder.model.UraModelLocator;
	import br.com.lh.urabuilder.view.ActionComponent;
	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class LoadUraCommand implements ICommand	{
		
		private var modelLocator:UraModelLocator =  UraModelLocator.getInstance();
		
		public function LoadUraCommand(){
		}
			
		public function execute(event:CairngormEvent):void{
			var dbServer:RemoteObject = new RemoteObject("dbDestination");
			var uraId :String =LoadUraEvent(event).uraId;
			var sessionId:String = LoadUraEvent(event).sessionId;
			dbServer.loadUra(uraId,sessionId);
			dbServer.loadUra.addEventListener("result",doLoad);	
		}
	
		private function doLoad(event:ResultEvent):void	{
			if (event.result == null){
				Alert.show("NAO ENCONTRADA");
				return;
			}
			CairngormEventDispatcher.getInstance().dispatchEvent(new NewUraEvent());
			var uraXml:UraXml = UraXml(event.result);
			modelLocator.resourceManager.availableExtension = uraXml.availableExtension;
			modelLocator.resourceManager.soundList = uraXml.soundFileList;
			insertComponents(uraXml);
			insertArrows(uraXml.xmlString);
			uraXml.clearViewArrays();
			uraXml.xml = new XML(uraXml.xmlString);			
			modelLocator.xmlManager.setUraXml(uraXml);
//			if (modelLocator.uraPropertiesForm !=null){
//				modelLocator.uraPropertiesForm.aliasText.text = uraXml.alias;
//				modelLocator.uraPropertiesForm.uraExtension.text = uraXml.uraExtension;
//			}			
		}
		
	
		private function insertComponents(uraXml:UraXml):void{
			var returnedXml:XML = new XML(uraXml.xmlString);
			var componentsXml:XMLList= returnedXml.children();
			for (var i:int = 0; i < componentsXml.length();i++){
				var type:String= XML(componentsXml[i]).localName() as String;
				var uraView:Object= uraXml.uraViewList.getItemAt(i);
				var component:ActionComponent = new ActionComponent();
				component.loadComponent(type,uraView.XPosition,	uraView.YPosition,uraView.elementId,uraView.id);
				component.uraAction.buildFromXml(componentsXml[i]);
				modelLocator.dragDropHelper.addListeners(component,type);
			 	CairngormEventDispatcher.getInstance().dispatchEvent(new AddActionEvent(component));
			 	CairngormEventDispatcher.getInstance().dispatchEvent(new SavePropertiesEvent(component));
			}
		}
	
	
		//TODO : IMPLEMENTAR DICTIONARY no VIEWMANAGERIMPL
		private function insertArrows(xml:String):void{
			var uraxml:XML = new XML(xml); 	
			var childrenXML:XMLList= uraxml.children();
			for each (var component:XML in childrenXML){
				var nextId:String = component['@nextId'];
				var startComponent:ActionComponent = modelLocator.viewManager.getComponentById(component['@id']);
				if (nextId == ""){
					var type:String= XML(component).localName() as String;
					if (type == "Conditional" || type=="WebService"){
						handleOptions(startComponent,component);						
					}
					continue;
				} 
				var endComponent:ActionComponent = modelLocator.viewManager.getComponentById(component['@nextId']);
				CairngormEventDispatcher.getInstance().dispatchEvent(new LinkActionEvent(startComponent,endComponent));
			}
			
		}
		
		private function handleOptions(element:ActionComponent,component:XML):void{
			var xmlList:XMLList = component.children();
			var optionList:ArrayCollection = new ArrayCollection();
			for each (var option:XML in xmlList){
				optionList.addItem(new Option(option));
			}
			CairngormEventDispatcher.getInstance().dispatchEvent(new SaveConditionalPropertiesEvent(element,optionList));
		}
		
	



	}
}